<?php

class Application_Model_Category {
    protected $_id;
    protected $_url;
    protected $_text;

    /********************************************************************/
    public function  __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }

    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);

        foreach ($options as $key => $value) {
            $method = "set" . ucfirst($key);

            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }

        return $this;
    }

    public function  __set($name,  $value)
    {
        $method = "set" . ucfirst($name);

        if (method_exists($this, $method)) {
            $this->$method($value);
        } else throw new Exception('oops) no such parameter to set');

    }

    public function __get($name)
    {
        $method = 'get' . ucfirst($name);

        if (method_exists($this, $method)) {
            return $this->$method($value);
        } else throw new Exception('oops) no such parameter to get');
    }

    /********************************************************************/
    
    public function setId($id)
    {
        // Максимум 3 цифри
        $regExp = '/^[0-9]{1,3}$/';
        if (!preg_match($regExp, $id)) throw new Exception("$id is not a valid category-id!");

        $this->_id = (int) $id;
        return $this;
    }

    public function getId()
    {
        return $this->_id;
    }

    /********************************************************************/

    /**
     * @return Application_Model_Category
     *
     */
    public function setUrl($url)
    {
        // Букви, цифри, тире. Починатись може лише з букви або цифри.
        $regExp = '/^'
                . '[a-zA-Z0-9]+'
                . '[a-zA-Z0-9-]*'
                . '[a-zA-Z0-9]'
                . '$/';
        if (!preg_match($regExp, $url)) throw new Exception('Invalid url!');

        $regExp = '/^[0-9]+$/';
        if (preg_match($regExp, $url)) throw new Exception('Category-url can not contain numbers only!');
        
        $this->_url = (string) $url;
        return $this;
    }

    public function getUrl()
    {
        return $this->_url;
    }

    /********************************************************************/
    public function setText($text)
    {
        $this->_text = (string) $text;
        return $this;
    }

    public function getText()
    {
        return $this->_text;
    }


}
?>
